﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.iRods.Common;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Diagnostics;
using Renci.iRods.Studio.Common.BaseClasses;
using Renci.iRods.Studio.Models;
using Renci.iRods.Data;
using Microsoft.Practices.ServiceLocation;
using Renci.iRods.Studio.Resources;
using Renci.iRods.Studio.Common;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;

namespace Renci.iRods.Studio.ViewModels
{
    public class ConnectionProfileManagerViewModel : ViewModelBase
    {
        private Profile _profile;

        private ConnectionProfile _newConnectionProfile;

        public IEnumerable<ConnectionProfile> ConnectionProfiles
        {
            get
            {
                yield return this._newConnectionProfile;

                foreach (var item in this._profile.ConnectionProfiles)
                {
                    yield return item;
                }
            }
        }

        private ConnectionProfile _selectedConnectionProfile;
        /// <summary>
        /// Gets or sets the selected connection profile.
        /// </summary>
        /// <value>
        /// The selected connection profile.
        /// </value>
        public ConnectionProfile SelectedConnectionProfile
        {
            get
            {
                return this._selectedConnectionProfile;
            }
            set
            {
                this._selectedConnectionProfile = value;

                this.NotifyOfPropertyChange(() => this.SelectedConnectionProfile);
            }
        }

        /// <summary>
        /// Gets the interaction request for displaying message dialog box
        /// </summary>
        public InteractionRequest<Notification> ShowMessageBoxRequest { get; private set; }

        public ConnectionProfileManagerViewModel()
        {
            this._profile = ServiceLocator.Current.GetInstance<Profile>();

            this._selectedConnectionProfile = this._newConnectionProfile = new ConnectionProfile() { Name = "<New>" };
            this.ShowMessageBoxRequest = new InteractionRequest<Notification>();

        }

        #region Commands

        /// <summary>
        /// Gets the test profile command.
        /// </summary>
        public ICommand TestProfileCommand
        {
            get { return new RelayCommand(() => this.TestProfile(), () => { return this.SelectedConnectionProfile != null && this.SelectedConnectionProfile.IsValid; }); }
        }

        /// <summary>
        /// Gets the delete profile command.
        /// </summary>
        public ICommand DeleteProfileCommand
        {
            get { return new RelayCommand(() => this.DeleteProfile(), () => { return this.SelectedConnectionProfile != null && this.SelectedConnectionProfile != this._newConnectionProfile; }); }
        }

        /// <summary>
        /// Gets the save profile command.
        /// </summary>
        public ICommand SaveProfileCommand
        {
            get { return new RelayCommand(() => this.SaveProfile(), () => { return this.SelectedConnectionProfile != null && this.SelectedConnectionProfile.IsValid; }); }
        }

        /// <summary>
        /// Gets the cancel profile command.
        /// </summary>
        public ICommand CancelProfileCommand
        {
            get { return new RelayCommand(() => this.CancelProfile()); }
        }

        #endregion

        private void SaveProfile()
        {
            if (this.SelectedConnectionProfile == this._newConnectionProfile)
            {
                this._profile.ConnectionProfiles.Add(this.SelectedConnectionProfile);
                this._newConnectionProfile = new ConnectionProfile() { Name = "<New>" };
            }

            this._profile.Save();

            this.NotifyOfPropertyChange(() => this.ConnectionProfiles);
        }

        private void DeleteProfile()
        {
            if (!this._profile.ConnectionProfiles.Remove(this.SelectedConnectionProfile))
            {
                throw new InvalidOperationException(string.Format("Profile '{0}' could not be deleted.", this.SelectedConnectionProfile.Name));
            }

            this._profile.Save();

            this.SelectedConnectionProfile = this._newConnectionProfile;

            this.NotifyOfPropertyChange(() => this.ConnectionProfiles);
        }

        private void TestProfile()
        {
            if (this.SelectedConnectionProfile != null && this.SelectedConnectionProfile.Test())
            {
                this.ShowMessageBoxRequest.Raise(new MessageDialogBox("Connection Test", "Connection was successful", MessageDialogBoxTypes.Information));

            }
            else
            {
                this.ShowMessageBoxRequest.Raise(new MessageDialogBox("Connection Test", "Connection was not successful", MessageDialogBoxTypes.Error));
            }
        }

        private void CancelProfile()
        {
            this.Close();
        }
    }
}
